﻿using System;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace Dynamic.Data.Model.Query
{
    [DataContract]
    public class ColumnSetDict : ColumnSetBase
    {
        private Dictionary<string, List<string>> _enityFields;
        private string _mainTable;

        public ColumnSetDict()
        {
            this._enityFields = new Dictionary<string, List<string>>();
            this._mainTable = string.Empty;
        }

        public ColumnSetDict(string mainTable)
        {
            this._enityFields = new Dictionary<string, List<string>>();
            this._mainTable = mainTable;
            this._enityFields[mainTable] = new List<string>();
        }

        public override void Accept(IQueryVisitor visitor)
        {
            visitor.Visit(this);
        }

        public void AddColumn(string entityName, string attributeName)
        {
            this._enityFields[entityName].Add(attributeName);
        }

        public void AddColumns(string entityName, params string[] attributeNames)
        {
            foreach (string str in attributeNames)
            {
                this._enityFields[entityName].Add(str);
            }
        }

        public void RemoveAllColumns()
        {
            this._enityFields.Clear();
        }

        public void RemoveColumn(string entityName, string name)
        {
            if (!this._enityFields.ContainsKey(entityName))
            {
                return;
            }

            for (int i = 0; i < this._enityFields[entityName].Count; i++)
            {
                if (this._enityFields[entityName][i].Equals(name))
                {
                    this._enityFields[entityName].RemoveAt(i);
                    return;
                }
            }
        }

        [DataMember]
        [XmlArray, XmlArrayItem(ElementName = "Attribute", IsNullable = false, Type = typeof(string))]
        public Dictionary<string, List<string>> EntityFields
        {
            get
            {
                return this._enityFields;
            }
            set
            {
                this._enityFields = value;
            }
        }

        [DataMember]
        public string MainTalbe
        {
            get
            {
                return this._mainTable;
            }
            set
            {
                this._mainTable = value;
            }
        }

        public override void AddColumn(string attributeName)
        {
            if (this._mainTable == string.Empty)
            {
                throw new Exception("请设置MainTalbew值");
            }
            if (this._enityFields[MainTalbe] == null)
            {
                this._enityFields[MainTalbe] = new List<string>();
            }

            this._enityFields[MainTalbe].Add(attributeName);
        }

        public override void AddColumns(params string[] attributeNames)
        {
            foreach (string str in attributeNames)
            {
                this.AddColumn(str);
            }
        }
    }
}
